<?php
class MY_Controller extends CI_Controller{
	
	function __construct(){
		parent::__construct();
	}
	
	/*
	 * $data 为数组
	 */
	function _output_json($data) {
		$this->load->library('jsontool');
		$this->output->set_output($this->jsontool->array2json($data));
	}
	
	/*
	 * _view
	 */
	function _view($path,$data=null) {
		if(isset($data))
			$this->load->view($path,$data);
		else 
			$this->load->view($path);
	}
	
	/**
	 * 
	 * @param $id
	 * @param $action
	 * @param $bean_file
	 * @param $data
	 */
	function _form($id,$action,$bean_file_path,$data=null) {
		include_once(APPPATH.'controllers'.$bean_file_path);
		$this->load->library('formview');
		return $this->formview->getForm("form_$id",site_url($action),$form_bean,$data);
	}
	
	function _pages($url,$total,$size,$index){
		$this->load->library('pagination');
		return $this->pagination->get_pages(site_url($url),$total,$size,$index);
	}
	
	/*
	 * 递归树
	 */
	function _findChild(&$arr,$id){
		$childs=array();
		foreach ($arr as $k =>$v){
			if($v['parentid']== $id){
				$childs[]=$v;
			}
		}
		return $childs;
	}
	
	function _build_tree($rows,$id=0){
		$childs=$this->_findChild($rows,$id);
		if(empty($childs)){
			return null;
		}
		foreach ($childs as $k => $v){
			$rescurTree= $this->_build_tree($rows,$v['id']);
			if( null != $rescurTree){
				$childs[$k]['childs']=$rescurTree;
			}
		}
		return $childs;
	}
}

class Auth_Controller extends MY_Controller{
	
	function __construct(){
		parent::__construct();
		$login = $this->session->userdata('login');
		if(!$login)
			redirect('login');
	}
}
?>